<?php

// No direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport( 'joomla.application.component.model' );

class RcbaseModelMotor extends JModel
{
	function getMotor($id=1)
	{
		$db =& JFactory::getDBO();
		$query = 'SELECT * FROM #__rcbase_motorlist WHERE id = '.$id;
		$db->setQuery( $query );
		$res = $db->loadAssoc();

		return $res;
	}
	function delMotor($id=null)
	{
		if ($id){
			$db =& JFactory::getDBO();
			$query = 'DELETE FROM #__rcbase_motorlist WHERE id = '.$id;
			$db->setQuery( $query );
			$db->query();
		}
	}

	function saveMotor()
	{
		$db =& JFactory::getDBO();
		$user =& JFactory::getUser();
		$motor_id = JRequest::getInt('motor_id');
		#die('ID: '.$motor_id);
		if ($motor_id){
			$query = "
				UPDATE #__rcbase_motorlist SET
					user_id = '".$user->id ."',
					product_id = '".JRequest::getInt('product_id')."',
					page_id = '".JRequest::getInt('page_id')."',
					title = '".JRequest::getVar('title')."',
					img_src = '".JRequest::getVar('img_src')."',
					kv = '".JRequest::getInt('kv', '0')."',
					weight = '".JRequest::getInt('weight', '0')."',
					max_current = '".JRequest::getInt('max_current', '0')."',
					max_voltage = '".JRequest::getInt('max_voltage', '0')."',
					memo = ".$db->quote(JRequest::getVar('memo'))."
				WHERE id = '".$motor_id."'
			";
		} else {
			$query = "
				INSERT INTO #__rcbase_motorlist SET
					user_id = '".$user->id ."',
					product_id = '".JRequest::getInt('product_id')."',
					page_id = '".JRequest::getInt('page_id')."',
					title = '".JRequest::getVar('title')."',
					img_src = '".JRequest::getVar('img_src')."',
					kv = '".JRequest::getInt('kv', '0')."',
					weight = '".JRequest::getInt('weight', '0')."',
					max_current = '".JRequest::getInt('max_current', '0')."',
					max_voltage = '".JRequest::getInt('max_voltage', '0')."',
					memo = ".$db->quote(JRequest::getVar('memo'))."
			";
		}
		#die('SQL: '.$query);
		$db->setQuery( $query );
		$db->query();
		if ($motor_id){
			return $motor_id;
		} else {
			return $db->insertid();
		}
	}

	function getMotorTest($id)
	{
		$db =& JFactory::getDBO();
		$query = 'SELECT * FROM #__rcbase_motortest WHERE motor_id = '.$id.' ORDER BY cells, power';
		$db->setQuery( $query );
		$res = $db->loadAssocList();
		return $res;
	}

	function getMotorSet($id)
	{
		$db =& JFactory::getDBO();
		$query = '
			SELECT s.*, t.title model_type_title FROM #__rcbase_motorset s
			JOIN #__rcbase_modeltype t ON t.id = s.model_type
			WHERE motor_id = '.$id.' ORDER BY cells, model_type, model_weight
		';
		$db->setQuery( $query );
		$res = $db->loadAssocList();
		return $res;
	}

	function getMotorTester($id)
	{
		$db =& JFactory::getDBO();
		$query = '
			SELECT DISTINCT user_id, username
			FROM #__rcbase_motortest t
			JOIN #__users u ON u.id = t.user_id
			WHERE t.motor_id = '.$id;
		$db->setQuery( $query );
		$res = $db->loadAssocList('user_id');
		return $res;
	}

	function getMotorRate($id){
		$db =& JFactory::getDBO();
		$query = "
			SELECT
				COUNT(DISTINCT u_up.id) rate_up,
				COUNT(DISTINCT u_dn.id) rate_down,
				GROUP_CONCAT(DISTINCT u_up.username SEPARATOR ', ') rate_up_users,
				GROUP_CONCAT(DISTINCT u_dn.username SEPARATOR ', ') rate_down_users
			FROM #__rcbase_motorlist m
			LEFT JOIN #__users u_up ON LOCATE(m.rate_up,u_up.id)
			LEFT JOIN #__users u_dn ON LOCATE(m.rate_down,u_dn.id)
			WHERE m.id = ".$id;
		$db->setQuery( $query );
		$res = $db->loadAssoc();
		return $res;
	}

	function setMotorRate($id, $voit){
		$db =& JFactory::getDBO();
		$user =& JFactory::getUser();
		# Гость не может голосовать по любому!
		if ($user->guest) return 'guest';
		$motor = $this->getMotor($id);
		# Проверяем что пользователь еще не голосовал
		if (strpos($motor['rate_up'].'|'.$motor['rate_down'], $user->id) === false){
			if ($voit == 'up'){ # Голосим ЗА!
				$query = "
					UPDATE #__rcbase_motorlist
					SET rate_up = IF(rate_up != '',CONCAT(rate_up, '|".$user->id."'),'".$user->id."')
					WHERE id = ".$id."
				";
				$db->setQuery( $query );
				$db->query();
				return 'up';
			} else { # Голосим ПРОТИВ!
				$query = "
					UPDATE #__rcbase_motorlist
					SET rate_down = IF(rate_down != '',CONCAT(rate_up, '|".$user->id."'),'".$user->id."')
					WHERE id = ".$id."
				";
				$db->setQuery( $query );
				$db->query();
				return 'down';
			}
		} else { # иначе возвращаем статус "Уже есть"
			return 'exist';
		}
	}
}